﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        private static int ia1 = 1;
        private static int ia2 = 1;
        private static int ia3 = 1;
        private static int ib1 = 1;
        private static int ib2 = 1;
        private static int ib3 = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            a1.Text = string.IsNullOrEmpty(a1.Text) ? "1" : a1.Text;
            a2.Text = string.IsNullOrEmpty(a2.Text) ? "1" : a2.Text;
            a3.Text = string.IsNullOrEmpty(a3.Text) ? "1" : a3.Text;
            b1.Text = string.IsNullOrEmpty(b1.Text) ? "1" : b1.Text;
            b2.Text = string.IsNullOrEmpty(b2.Text) ? "1" : b2.Text;
            b3.Text = string.IsNullOrEmpty(b3.Text) ? "1" : b3.Text;
            CellNum(a1.Text, a2.Text, a3.Text);
            CellNum(b1.Text, b2.Text, b3.Text, "b");
            try
            {
                int yup = ia3 * ib1 - ib3 * ia1;
                int ydown = ia2 * ib1 - ib2 * ia1;
                int xup = ia3 * ib2 - ib3 * ia2;
                int xdown = ia1 * ib2 - ib1 * ia2;
                //  y = (da3 * db1 - db3 * da1) / (da2 * db1 - db2 * da1);
                // x = (da3 - da2 * y) / da1;
                // label9.Text = $"x={x}   ,y={y}";
                string y = GetResult(yup, ydown);
                string x = GetResult(xup, xdown);
                label9.Text = $"x={x}   ,y={y}";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public string GetResult(int r1, int r2)
        {
            int temp = GCD(r1, r2);
            do
            {
                temp = GCD(r1 < 0 ? -r1 : r1, r2 < 0 ? -r2 : r2);
                r1 = r1 / temp;
                r2 = r2 / temp;
            } while (temp > 1);
            if (r2 == 1) return r1.ToString();
            if (r1 < 0 && r2 < 0)
            {
                r1 = -r1;
                r2 = -r2;
            }
            else if (r1 > 0 && r2 < 0)
            {
                r1 = -r1;
                r2 = -r2;
            }
            return $"{r1}/{r2}";
        }
        private void CellNum(string s1, string s2, string s3, string flag = "a")
        {
            int t2 = GetTimes(GetNum(s1));
            int t4 = GetTimes(GetNum(s2));
            int t6 = GetTimes(GetNum(s3));

            int t1 = GetUp(GetNum(s1));
            int t3 = GetUp(GetNum(s2));
            int t5 = GetUp(GetNum(s3));
            if (flag.Equals("a"))
            {
                ia1 = t1 * t4 * t6;
                ia2 = t2 * t3 * t6;
                ia3 = t2 * t4 * t5;
            }
            else
            {
                ib1 = t1 * t4 * t6;
                ib2 = t2 * t3 * t6;
                ib3 = t2 * t4 * t5;
            }
        }

        /// <summary>
        /// 最大公约数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int GCD(int a, int b)
        {
            int gcd = 1;
            int min = a > b ? b : a;
            for (int i = min; i >= 1; i--)
            {
                if (a % i == 0 && b % i == 0)
                {
                    gcd = i;
                    break;
                }
            }
            return gcd;
        }

        private int GetUp(string s1)
        {
            int times = 1;
            if (s1.Contains('/'))
            {
                string s = s1.Substring(0, s1.IndexOf('/'));
                times = Convert.ToInt32(s);
                return times;
            }
            return Convert.ToInt32(s1);
        }

        private int GetTimes(string s1)
        {
            int times = 1;
            if (s1.Contains('/'))
            {
                string s = s1.Substring(s1.IndexOf('/') + 1);
                times = Convert.ToInt32(s);
            }
            return times;
        }

        private string GetNum(string s1)
        {
            if (s1.Contains('.'))
            {
                string s = s1.Substring(s1.IndexOf('.') + 1);
                double d1 = Convert.ToDouble(s1);
                double d2 = Math.Pow(10, s.Length);
                int i = Convert.ToInt32(d1 * d2);
                return $"{i}/{d2}";
            }
            return s1;
        }
    }
}
